package com.zw.yzk.kabu.view.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.QRCodeModel;
import com.zw.yzk.kabu.utils.ResUtils;

import butterknife.Bind;

/**
 * Created by zhanwei on 2017/2/11.
 */

public class TicketQrCodeActivity extends BaseActivity {

    @Bind(R.id.ticket_name)
    TextView ticketName;
    @Bind(R.id.ticket_count)
    TextView ticketCount;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.ticket_id)
    TextView ticketId;
    @Bind(R.id.qr_code)
    ImageView qrCode;
    @Bind(R.id.invalid)
    ImageView invalid;
    @Bind(R.id.time)
    TextView time;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ticket_qr_code;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.ticket_qr_code))
                .build();
    }

    @Override
    public void initComponent() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QRCodeModel qrCodeModel = getIntent().getParcelableExtra(ActivityConstant.QR_CODE_DATA);
        ticketName.setText(qrCodeModel.getTitle());
        ticketCount.setText(getResources().getString(R.string.electronic_ticket_count, qrCodeModel.getCount()));
        time.setText(qrCodeModel.getTime());
        price.setText(ResUtils.formatCurrency(this, qrCodeModel.getTotalPrice()));
        String id = qrCodeModel.getSerialNumber() + qrCodeModel.getCheckCode();
        ticketId.setText(id);
        createQrCode(qrCodeModel.getSerialNumber(), qrCodeModel.getCheckCode());

        invalid.setVisibility(qrCodeModel.isInvalid() ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 生成二维码
     *
     * @param serialNumber
     * @param checkCode
     */
    private void createQrCode(String serialNumber, String checkCode) {
        int qrCodeWidth = getResources().getDimensionPixelSize(R.dimen.ticket_qr_code_width);
        String tmp = serialNumber + "|" + checkCode;
        Bitmap mBitmap = CodeUtils.createImage(tmp, qrCodeWidth, qrCodeWidth, null);
        qrCode.setImageBitmap(mBitmap);
    }
}
